﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.SharePoint;

namespace SPContrib.SharePoint.Extensions
{
    /// <summary>
    /// Extensions for the SPList class
    /// </summary>
    public static class SPListExtensions
    {
        ///// <summary>
        ///// Ensures that the SPList has a folder that meets the name criteria.
        ///// It will create the folder if it´s not found on the SPList
        ///// </summary>
        ///// <param name="name">the name of the folder</param>
        //public static SPFolder EnsureFolder(this SPList instance, string name)
        //{
        //    SPFolder result;
        //    if (instance.TryFindFolderByName(name, out result) == false)
        //    {
        //        result = instance.Folders.Add(name, SPFileSystemObjectType.Folder).Folder;
        //        instance.Update();
        //    }

        //    return result;
        //}

//        /// <summary>
//        /// Finds a file by the name of the file (e.g. 'somefile.txt' or 'somefile')
//        /// </summary>
//        /// <param name="name">the name of the SPFile to query for</param>
//        public static SPFile FindFileByName(this SPList instance, string name)
//        {
//           SPFile match = default(SPFile);            
//           name = Path.GetFileNameWithoutExtension(name);

//            // TODO: use LINQ provider for CAML translation when it´s finished
//            string caml = @"
//<Where>
//    <Contains>
//        <FieldRef Name='FileLeafRef' />
//        <Value Type='File'>{0}</Value>
//    </Contains>
//</Where>".Trim();

//            var result = instance.GetItems(new SPQuery
//            {
//                RowLimit = 1,
//                Query = string.Format(caml, name),
//                ViewAttributes = "Scope='Recursive'" // search subfolders
//            });

//            if (result.Count == 1)
//            {
//                match = result[0].File;
//            }

//            return match;
//        }

        /// <summary>
        /// Query the SPList for folders that meets the search criteria
        /// </summary>
        /// <param name="criteria">the search criteria delegate (e.g. folder => folder.Name == "SomeFolder")</param>
        public static IEnumerable<SPFolder> FindFoldersByCriteria(this SPList instance, Predicate<SPFolder> criteria)
        {
            // TODO: use LINQ provider when finished?
            return from SPFolder f in instance.Folders
                   where criteria(f)
                   select f;
        }

        ///// <summary>
        ///// Query the SPList for folders that meet the name criteria
        ///// </summary>
        ///// <param name="name">the name of the folder</param>
        //public static IEnumerable<SPFolder> FindFoldersByName(this SPList instance, string name)
        //{
        //    return instance.FindFoldersByCriteria(folder => folder.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
        //}

        /// <summary>
        /// Returns the name (i.e. internal name) of the SPList
        /// </summary>
        public static string Name(this SPList instance)
        {
            return instance.RootFolder.Name;
        }

        ///// <summary>
        ///// Finds a file by the name of the file (e.g. 'somefile.txt' or 'somefile'). 
        ///// A boolean indicates if the query was successful or not.
        ///// </summary>
        ///// <param name="name">the name of the SPFile to query for</param>
        ///// <param name="result">the SPFile to return if the query is successful</param>
        //public static bool TryFindFileByName(this SPList instance, string name, out SPFile result)
        //{
        //    result = instance.FindFileByName(name);
        //    return result.HasValue<SPFile>();
        //}

        ///// <summary>
        ///// Query the SPList for folders that meet the name criteria. 
        ///// A boolean indicates if the query was successful or not.
        ///// </summary>
        ///// <param name="name">the name of the folder</param>
        ///// <param name="result">the SPFolder to return if the query is successful</param>
        //public static bool TryFindFolderByName(this SPList instance, string name, out SPFolder result)
        //{
        //    if (instance.RootFolder.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
        //    {
        //        result = instance.RootFolder;
        //        return true;
        //    }

        //    bool match = instance
        //                    .FindFoldersByName(name)
        //                    .Execute(out result);

        //    return match;
        //}
    }
}
